package thread.thread_local;

/**
 * @program: Multhread
 * @description: 通过弱引用, 将内存泄漏问题解决, 通过weakReference
 * @author: zhanghz001
 * @create: 2021-03-05 10:34
 **/
public class ThreadLocalDemo2 {
    //ThreadLocalMap.Entry继承了引用,
    public static void main(String[] args) {
        ThreadLocal<Integer> local = new ThreadLocal<>();
        local.set(100);
        System.out.println(local.get());
        System.gc();
        //不会被回收,local被引用了
        System.out.println(local.get());
        local = null;
        //currentThread中的localMaps
        //注意table的reference
        Thread currentThread = Thread.currentThread();
        //断点1:虽然local被赋值null,但是ThreadLocal内部存在引用(内存泄漏风险)
        System.out.println(1);
        System.gc();
        //断点2,gc后,引用消除
        System.out.println(2);
    }
}
